﻿using System;
using System.Globalization;
using System.Windows.Data;
using Loki.Utils;

namespace Loki.UI.Wpf
{
    public class CommandToTileConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            bool first;
            BindableCollection<TileViewModel> flatCollection = new BindableCollection<TileViewModel>();
            BindableCollection<CommandGroupViewModel> Groups = value as BindableCollection<CommandGroupViewModel>;
            if (Groups != null)
            {
                foreach (CommandGroupViewModel group in Groups)
                {
                    first = true;
                    foreach (var tile in group.Commands)
                    {
                        if (first)
                            flatCollection.Add(new TileViewModel() { Header = tile.DisplayName, Command = tile.Command, GroupHeader = group.Name, IsFlowBreak = true });
                        else
                            flatCollection.Add(new TileViewModel() { Header = tile.DisplayName, Command = tile.Command, });
                        first = false;
                    }
                }
            }

            return flatCollection;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value;
        }
    }
}